<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1_引出生命周期</title>
</head>
<body>
  <!-- 准备好一个“容器” -->
  <div id="test"></div>

  <!-- 引入react核心库 -->
  <script type="text/javascript" src="../js/react.development.js"></script>
  <!-- 引入react-dom，用于支持react操作DOM -->
  <script type="text/javascript" src="../js/react-dom.development.js"></script>
  <!-- 引入babel，用于将jsx转为js -->
  <script type="text/javascript" src="../js/babel.min.js"></script>

  <!-- 此处一定要写babel -->
  <script type="text/babel">
    // 1. 创建组件
    // 生命周期回调函数 <=> 生命周期钩子函数 <=> 生命周期函数 <=> 生命周期钩子
    class Lifecycle extends React.Component {
      // 初始化状态
      state= {
        opacity: 0.6
      }

      handleChange = () => {
        // 清除定时器
        // clearInterval(this.timer)
        // 卸载组件
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }

      // 组件挂载完毕
      componentDidMount() {
        console.log('componentDidMount')
        this.timer = setInterval(() => {
          // 获取原状态
          let {opacity} = this.state
          opacity -= 0.1
          if(opacity <= 0) opacity = 1
          this.setState({opacity})
        }, 200)
      }

      // 组件将要卸载
      componentWillUnmount() {
        console.log('componentWillUnmount')
        // 清除定时器
        clearInterval(this.timer)
      }

      // 初始化渲染、状态更新之后
      render() {
        console.log('render')
        return (
          <div>
            <h2 style={{opacity: this.state.opacity}}>React 学习</h2>
            <button onClick={this.handleChange}>切换</button>
          </div>
        )
      }
    }
    // 2. 渲染组件到页面
    ReactDOM.render(<Lifecycle />, document.getElementById('test'))
  </script>
</body>
</html>